﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Win32;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft;
using System.Drawing;
using CloudOfficeEngine;
using GoogleConnector;
using Utilities;
using UILibrary;

namespace OfficeInCloudExcel2003
{

    public partial class ThisAddIn
    {
        Excel.Application m_app;

        private ExcelActionPane m_pane;
        private COE m_Engine;
        private ExcelLocalView m_LocalView;
        private Connector m_RemoteConnector;
        public MyGDocsForm m_MyGDocsForm;
        private bool DisabledMode = false;
        private TraceWrapper m_traceWrapper;
        private DbOperations m_dbOperations;


        private void CommunicationSetup()
        {
            m_LocalView = new ExcelLocalView(this.Application, m_dbOperations);
            m_RemoteConnector = new Connector();
            m_Engine = new COE(m_LocalView, m_RemoteConnector);
        }

        public void traceSystemInfo()
        {
            //write header
            Trace.WriteLine("Operating system: " + System.Environment.OSVersion.ToString());
            Trace.WriteLine("CLR runtime version: " + System.Environment.Version.ToString());
            Trace.WriteLine("Excel version: " + this.Application.Version);
            Trace.WriteLine("Application info: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString());
        }

        private void initPaths()
        {
            Utilities.Path.logPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Uprise Apps\\Excel2007Log.txt";
            Utilities.Path.dbPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Uprise Apps\\Info.mdb";
        }


        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            #region VSTO generated code

            this.Application = (Excel.Application)Microsoft.Office.Tools.Excel.ExcelLocale1033Proxy.Wrap(typeof(Excel.Application), this.Application);

            #endregion
            m_app = this.Application;
            
            m_app.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(app_WorkbookOpen);
            ((Microsoft.Office.Interop.Excel.AppEvents_Event)m_app).NewWorkbook += new Microsoft.Office.Interop.Excel.AppEvents_NewWorkbookEventHandler(app_WorkbookNew);
            
            initPaths();

            m_traceWrapper = new TraceWrapper();
            m_traceWrapper.initTraceListener();

            traceSystemInfo();

            //m_db = new DbManager(Path.dbPath);
            m_dbOperations = new DbOperations(new DbManager(Utilities.Path.dbPath));

            //First Run
            RegistryKey kHKCU = Registry.CurrentUser;
            RegistryKey kFirstRun;
            try
            {
                kFirstRun = kHKCU.OpenSubKey("Software\\UpriseApps\\OfficeInCloudExcel2007", true);
                if (kFirstRun == null)
                {
                    MessageBox.Show("Office In Cloud for Google Docs (Excel 2007) was not properly installed. \n Please contact Uprise Apps support.", "Office In Cloud Error Message");
                }
                else
                {
                    if (kFirstRun.GetValue("FirstRun").ToString().Equals("1"))
                    {
                        //Registration Wizard
                        Registration regForm = new Registration(m_dbOperations);
                        if (DialogResult.OK == regForm.ShowDialog())
                        {
                            DisabledMode = false;
                        }
                    }
                    else DisabledMode = false;
                }
            }
            catch (Exception ex)
            {
                Utilities.TraceWrapper.LogException("ThisAddIn_Startup error: " + ex.Message, ex);

                MessageBox.Show("Office In Cloud for Google Docs (Excel 2007) was not properly installed. \n Please contact Uprise Apps support.", "Office In Cloud Error Message");
            }

            CommunicationSetup();

            ImageList m_ImageList = new ImageList();
            m_ImageList.ColorDepth = ColorDepth.Depth32Bit;
            m_ImageList.TransparentColor = Color.Transparent;
            m_ImageList.ImageSize = new Size(20, 20);


            m_ImageList.Images.Add(Properties.Resources.UpriseApps);
            m_ImageList.Images.Add(Properties.Resources.MessageQueuing);
            m_ImageList.Images.Add(Properties.Resources.users);
            m_ImageList.Images.Add(Properties.Resources.Folder);
            m_ImageList.Images.Add(Properties.Resources.update_small);
            m_ImageList.Images.Add(Properties.Resources.support_small);

            m_MyGDocsForm = new MyGDocsForm(m_LocalView, DisabledMode, m_dbOperations, m_traceWrapper, Google.Documents.Document.DocumentType.Spreadsheet);
            //m_MyGDocsForm.AutoSize = true;
            m_MyGDocsForm.MyGDocsTabControl.ImageList = m_ImageList;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["SignIn"].ImageIndex = 0;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["ShareTab"].ImageIndex = 2;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["Explorer"].ImageIndex = 3;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["UpdateTab"].ImageIndex = 4;
            m_MyGDocsForm.MyGDocsTabControl.TabPages["SupportTab"].ImageIndex = 5;

            //create our own window at the top of command bar "Task Pane"
            m_pane = new ExcelActionPane(m_app);
            m_pane.buildActionPane();

            m_pane.m_actionPane.Controls.Add(m_MyGDocsForm);

            m_MyGDocsForm.Dock = DockStyle.Fill;
            m_MyGDocsForm.Visible = true;
        }

        void app_WorkbookNew(Microsoft.Office.Interop.Excel.Workbook Wb)
        {
            m_pane.resizeActionPane(313);
        }

        void app_WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook Wb)
        {
            m_pane.resizeActionPane(313);
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            //restore wnd proc
            m_pane.release();

            // Flush the output.
            Trace.WriteLine("");
            Trace.WriteLine("");
            Trace.WriteLine("");
            m_traceWrapper.shutdownTraceListener();

        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
